<template>
  <div class="sidebar-content">
    <h3>设计模板库</h3>
    <div class="template-grid">
      <div 
        v-for="template in designTemplates" 
        :key="template.name" 
        class="template-item"
        @click="$emit('applyDesignTemplate', template)"
      >
        <img :src="template.thumbnail" :alt="template.name">
        <div class="template-name">{{ template.name }}</div>
      </div>
    </div>
    
    <h3>商业模板</h3>
    <div class="template-section">
      <div class="template-card" @click="$emit('applyTemplate', {name:'电商Banner'})">
        <div class="template-icon">🛒</div>
        <div class="template-title">电商Banner</div>
      </div>
      <div class="template-card" @click="$emit('applyTemplate', {name:'促销海报'})">
        <div class="template-icon">🏷️</div>
        <div class="template-title">促销海报</div>
      </div>
      <div class="template-card" @click="$emit('applyTemplate', {name:'产品展示'})">
        <div class="template-icon">📱</div>
        <div class="template-title">产品展示</div>
      </div>
    </div>
    
    <h3>社交媒体模板</h3>
    <div class="template-section">
      <div class="template-card" @click="$emit('applyTemplate', {name:'朋友圈封面'})">
        <div class="template-icon">📱</div>
        <div class="template-title">朋友圈封面</div>
      </div>
      <div class="template-card" @click="$emit('applyTemplate', {name:'微博卡片'})">
        <div class="template-icon">🔥</div>
        <div class="template-title">微博卡片</div>
      </div>
      <div class="template-card" @click="$emit('applyTemplate', {name:'视频封面'})">
        <div class="template-icon">🎬</div>
        <div class="template-title">视频封面</div>
      </div>
    </div>
    
    <h3>新建空白画布</h3>
    <div class="blank-templates">
      <div class="blank-template" @click="$emit('select-ratio', {width: 1920, height: 1080})">
        <div class="blank-preview">16:9</div>
        <div class="blank-title">横幅 (1920×1080)</div>
      </div>
      <div class="blank-template" @click="$emit('select-ratio', {width: 1080, height: 1920})">
        <div class="blank-preview">9:16</div>
        <div class="blank-title">手机 (1080×1920)</div>
      </div>
      <div class="blank-template" @click="$emit('select-ratio', {width: 1200, height: 1200})">
        <div class="blank-preview">1:1</div>
        <div class="blank-title">方形 (1200×1200)</div>
      </div>
    </div>

    <h3>创建自定义尺寸</h3>
    <div class="custom-size">
      <div class="size-inputs">
        <div class="size-input-group">
          <label>宽度 (像素)</label>
          <input type="number" v-model="customWidth" min="100" max="4000">
        </div>
        <div class="size-input-group">
          <label>高度 (像素)</label>
          <input type="number" v-model="customHeight" min="100" max="4000">
        </div>
      </div>
      <button class="create-button" @click="createCustomCanvas">创建画布</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TemplatePanel',
  props: {
    designTemplates: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      customWidth: 800,
      customHeight: 600
    }
  },
  methods: {
    createCustomCanvas() {
      // 验证输入
      const width = parseInt(this.customWidth);
      const height = parseInt(this.customHeight);
      
      if (!width || !height || width < 100 || height < 100 || width > 3000 || height > 3000) {
        alert('请输入有效的尺寸（宽度和高度均需在100-3000像素之间）');
        return;
      }
      
      // 发送事件到父组件
      this.$emit('select-ratio', { width, height });
      
      // 清空输入
      this.customWidth = 800;
      this.customHeight = 600;
    }
  }
}
</script>

<style scoped>
.sidebar-content {
  padding: 12px;
  overflow-y: auto;
  height: 100%;
  background-color: #f9fafc;
  scrollbar-width: thin;
}

/* 优化滚动条样式 */
.sidebar-content::-webkit-scrollbar {
  width: 6px;
}

.sidebar-content::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-content::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.15);
  border-radius: 3px;
}

.sidebar-content::-webkit-scrollbar-thumb:hover {
  background-color: rgba(0, 0, 0, 0.25);
}

h3 {
  font-size: 15px;
  margin-bottom: 12px;
  margin-top: 20px;
  color: #262626;
  font-weight: 600;
  position: relative;
  padding-left: 10px;
  letter-spacing: 0.2px;
}

h3:first-child {
  margin-top: 6px;
}

h3::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  background: linear-gradient(to bottom, #1890ff, #36cfc9);
  border-radius: 2px;
}

/* 简化标题下方的指示条 */
h3::after {
  content: "";
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  bottom: -6px;
  height: 1px;
  background: linear-gradient(to right, rgba(24, 144, 255, 0.1), rgba(54, 207, 201, 0));
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.template-item {
  border: 1px solid #eaeaea;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  background-color: #fff;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
}

.template-item:hover {
  transform: translateY(-2px);
  border-color: #1890ff;
  box-shadow: 0 3px 8px rgba(24, 144, 255, 0.12);
}

.template-item img {
  width: 100%;
  height: auto;
  display: block;
  transition: all 0.3s;
}

.template-item:hover img {
  filter: brightness(1.05);
}

.template-name {
  padding: 8px;
  font-size: 12px;
  color: #555;
  text-align: center;
  border-top: 1px solid #f0f0f0;
  background-color: #fafafa;
  transition: all 0.2s;
}

.template-item:hover .template-name {
  background-color: #e6f7ff;
  color: #1890ff;
}

.template-section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.template-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 90px;
  padding: 10px;
  border: 1px solid #eaeaea;
  border-radius: 6px;
  background-color: #fff;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
}

.template-card:hover {
  transform: translateY(-2px);
  border-color: #1890ff;
  background-color: #f0f9ff;
  box-shadow: 0 3px 8px rgba(24, 144, 255, 0.12);
}

.template-icon {
  font-size: 24px;
  margin-bottom: 8px;
  transition: transform 0.2s ease;
}

.template-card:hover .template-icon {
  transform: scale(1.1);
  color: #1890ff;
}

.template-title {
  font-size: 12px;
  color: #555;
  text-align: center;
  transition: color 0.2s;
}

.template-card:hover .template-title {
  color: #1890ff;
}

.blank-templates {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.blank-template {
  border: 1px solid #eaeaea;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  background-color: #fff;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
}

.blank-template:hover {
  transform: translateY(-2px);
  border-color: #1890ff;
  box-shadow: 0 3px 8px rgba(24, 144, 255, 0.12);
}

.blank-preview {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: #bfbfbf;
  background: linear-gradient(135deg, #f5f5f5 25%, #fafafa 25%, #fafafa 50%, #f5f5f5 50%, #f5f5f5 75%, #fafafa 75%, #fafafa 100%);
  background-size: 28px 28px;
  border-bottom: 1px dashed #eaeaea;
  transition: all 0.3s;
}

.blank-template:hover .blank-preview {
  color: #1890ff;
  background: linear-gradient(135deg, #e6f7ff 25%, #f0f9ff 25%, #f0f9ff 50%, #e6f7ff 50%, #e6f7ff 75%, #f0f9ff 75%, #f0f9ff 100%);
  background-size: 28px 28px;
}

.blank-title {
  padding: 8px;
  font-size: 12px;
  color: #555;
  text-align: center;
  background-color: #fafafa;
  transition: all 0.2s;
}

.blank-template:hover .blank-title {
  background-color: #e6f7ff;
  color: #1890ff;
}

.custom-size {
  border: 1px solid #eaeaea;
  border-radius: 6px;
  padding: 14px;
  background-color: #fafafa;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
  margin-bottom: 20px;
}

.size-inputs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

.size-input-group {
  display: flex;
  flex-direction: column;
}

.size-input-group label {
  font-size: 12px;
  color: #555;
  margin-bottom: 5px;
}

.size-input-group input {
  height: 32px;
  padding: 0 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  transition: all 0.2s;
}

.size-input-group input:hover {
  border-color: #40a9ff;
}

.size-input-group input:focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
  outline: none;
}

.create-button {
  width: 100%;
  height: 34px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.create-button:hover {
  background-color: #40a9ff;
  box-shadow: 0 3px 8px rgba(24, 144, 255, 0.2);
}

.create-button:active {
  background-color: #096dd9;
}

/* 响应式调整：在较小的视口高度下压缩UI */
@media (max-height: 768px) {
  h3 {
    margin-top: 16px;
    margin-bottom: 10px;
    font-size: 14px;
  }
  
  .template-section, .template-grid, .blank-templates {
    margin-bottom: 16px;
  }
  
  .template-card {
    height: 80px;
  }
  
  .blank-preview {
    height: 50px;
  }
  
  .custom-size {
    padding: 12px;
    margin-bottom: 16px;
  }
}
</style> 